package pt;

/**
 * @author wcj
 * @Date 2021/7/6 23:20
 * @Version 1.0
 */
public class BinarySearch {

    /**
     * 二分查找，有返回下标，没有返回-1
     *
     * @param arr
     * @param target
     * @return
     */
    public static int pt(int[] arr, int target) {
        int low = 0;
        int high = arr.length - 1;
        while (low < high) {
            int mid = (low + high) / 2;
            if (target < arr[mid]) {
                high = mid - 1;
            } else if (target > arr[mid]) {
                low = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}
